// pages/real/wholevideo/index.js
const app = getApp()
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    rows:20,
    compreFlag:true,
    hotFlag:false,
    showPopup:false,
    isRefresh:false,
    hasNoMore:false,
    navHeight: app.globalData.navHeight,
    styleFilterList:[],
    browseNum:null,
    subStr:{},
    contentTop:"",
    leftList:[],
    rightList:[],
    userInfo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var query = wx.createSelectorQuery()
    query.select('.home_c_filter').boundingClientRect((rect)=>{
      console.log(rect)
      this.setData({
       contentTop:rect.height+app.globalData.navHeight
      })
    }).exec();
    this.handleGetAllVideoList()
    this.handleGetAllDictList()
  }, 
  //供投稿结果页面调用
  // handleInitSearch(){
  //   this.handleGetAllDictList()
  // },
  toLoginPage() {
    let message = ''
    if(!this.data.userInfo) {
      message = '未登陆，是否跳到登陆页登陆'
    } 
    wx.showModal({
      title: '提示',
      content: message,
      success:function(res) {
        if(res.confirm){
          wx.navigateTo({
            url: '/pages/mine/register/index'
          });
        }
      }
    })
  },
  /**
   * 获取本地用户信息
  */
  getLocalUserInfo: function(){
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo){
      this.setData({ userInfo, phone: userInfo.phone })
    }
  },
  goContriPage(){
    const userInfo = wx.getStorageSync("userInfo")
    const phone = wx.getStorageSync('phone')
    if(!userInfo){
      this.toLoginPage()
    }else{
    wx.navigateTo({
      url: '/pages/real/contribute/index',
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        // res.eventChannel.emit('casepage', { data })
      }
    })
    }
  },
  indexConditFitler(option){
    const {name,item,list}=option.currentTarget.dataset
    list.forEach(l=>{
      if(l.id===item.id){
        l.flag=!l.flag
      }else{
        l.flag=false
      }
    })
    this.setData({
      [name]:list
    })
  },
  getFilterConditToSearch(){
      const {styleFilterList} =this.data
    // const canContinue=styleFilterList.some(item=>item.flag)
    // if(!canContinue){
    //   wx.showLoading({
    //     title:  '请选择风格'
    //   })
    //   setTimeout(function () {
    //     wx.hideLoading()
    //   }, 1000)
    //   return
    // }
    let str={}
    styleFilterList.forEach(item=>{
      if(item.flag){
        str.styleCode=item.code
        str.styleName=item.name
      }
    })
    this.setData({
      subStr:str
    })
    // this.cancelSelected()
    this.onClose()
    this.handleInitSearch()
  },
  cancelSelected(){
    // const {styleFilterList} =this.data
    // this.setData({
    //   styleFilterList:styleFilterList.map(item=>{
    //     item.flag=false
    // return item
    //   })
    // })
    this.onClose()
  },
  handleGetAllDictList(){
    utils.$http(app.globalData.api.getRealVeidoAllStyle, {}, 'GET', false, 'JSON').then(res => {
      if(res.success){
      const  styles=res.data.map(dd=>{
        dd.flag=false
        dd.name=dd.styleName
        return dd
      })
      this.setData({
        styleFilterList:styles,
      })
      }
    })
  },
  showFilterPage(){
    this.setData({
      showPopup:true
    })
  },
  onClose(){
    this.setData({
      showPopup:false
    })
  },
  handleInitSearch(){
    this.setData({
      compreFlag:true,
      hotFlag:false
    })
    this.handlePubConfigSet()
    this.handleGetAllVideoList()
  },
  handleSearchHot(){
    this.setData({
    compreFlag:false,
    hotFlag:true
  })
    this.handlePubConfigSet('hot')
    this.handleGetAllVideoList()
  },
  handlePubConfigSet(val){
    const hots={
      browseNum:1
   }
    const str={
      leftList:[],
      rightList:[],
      page:1,
      rows:20,
    }
    if(val==='hot'){
       this.setData({
      ...str,
      ...hots
    })
    }else{
      console.log('来了老弟')
      this.setData({
        ...str,
        browseNum:null
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  loadNewData: function() {
    this.setData({
      isRefresh :true
    })
    this.data.page = 1
    this.handleGetAllVideoList()
  },
  loadMoreData:function() {
    console.log(123)
    this.setData({
      page: this.data.page + 1
    })
    this.handleGetAllVideoList(true)
  },
  handleGetAllVideoList(flag=false){
    const {page,rows,subStr,browseNum} =this.data
    const list={
      page,
      rows,
      approveState:1,
      upState:0,
      browseNum,
      ...subStr,
      tagCode:'SAMPLE_ROOM_VIDEO'
    }
    utils.$http(app.globalData.api.getRealVeidoDataList, list, 'POST', true, 'JSON').then(res => {
      const {leftList=[],rightList=[]} =this.data
      if(res.success){
        const data=res.data.records
        if(flag){
          if(data.length===0){
            this.setData({
              hasNoMore:true
            })
          }else{
            if(data.length===1){
             this.setData({
              isRefresh: false,
               leftList:leftList.concat(data)
             })
            }else{
              data.forEach((item,index)=>{
                if((index+1)%2===0){
                  rightList.push(item)
                }else{
                  leftList.push(item)
                }
              })
              this.setData({
                isRefresh: false,
                rightList,
                leftList
                // browseNum:null
              })
            }
          }
        }else{
          const lList=[],rList=[];
          data.forEach((item,index)=>{
            if((index+1)%2===0){
              rList.push(item)
            }else{
              lList.push(item)
            }
          })
          this.setData({
            isRefresh:false,
            hasNoMore: false,
            leftList: lList,
            rightList:rList
          })
        }
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})